/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package limousine.dbservice;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import limousine.util.Func;
import limousine.vo.CarRent;

/**
 *
 * @author Tailer
 */
public class CarRentService {

    private DataSource ds = null;
    private Func func = new Func();

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    public CarRent getByTableId(String tableId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * From CAR_RENT where TABLE_ID = '" + tableId.trim() + "' ");
        CarRent o = null;
        try {
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql.toString());
            if (rs.next()) {
                o = new CarRent();
                o.setTableId(rs.getString("TABLE_ID"));
                o.setCarRentRateId(rs.getString("CAR_RENRATE_ID"));
                o.setPerHour(rs.getInt("PER_HOUR"));
                o.setMinimumCharge(rs.getInt("MINIMUM_CHARGE"));
                o.setSedan(rs.getInt("SEDAN"));
                o.setVan(rs.getInt("VAN"));
                o.setTrip(rs.getInt("TRIP"));
                o.setServiceType(rs.getInt("SERVICE_TYPE"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(rs.getDate("CREATE_DATE"));
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(rs.getDate("UPDATE_DATE"));
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    public boolean insert(CarRent o) {
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO CAR_RENT(TABLE_ID,CAR_RENRATE_ID,PER_HOUR,MINIMUM_CHARGE,SEDAN,VAN,TRIP,SERVICE_TYPE, ");
        sql.append("CREATE_BY,CREATE_DATE,UPDATE_BY,UPDATE_DATE ) ");
        sql.append("VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getTableId());
            stmt.setString(2, o.getCarRentRateId());
            stmt.setInt(3, o.getPerHour());
            stmt.setInt(4, o.getMinimumCharge());
            stmt.setInt(5, o.getSedan());
            stmt.setInt(6, o.getVan());
            stmt.setInt(7, o.getTrip());
            stmt.setInt(8, o.getServiceType());
            stmt.setString(9, o.getCreateBy());
            stmt.setTimestamp(10, func.getTimestamp());
            stmt.setString(11, o.getUpdateBy());
            stmt.setTimestamp(12, func.getTimestamp());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean update(CarRent o) {
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE CAR_RENT ");
        sql.append("SET CAR_RENRATE_ID=?,PER_HOUR=?,MINIMUM_CHARGE=?,SEDAN=?,VAN=?,TRIP=?,SERVICE_TYPE=?, ");
        sql.append("UPDATE_BY=?,UPDATE_DATE=? ");
        sql.append("where TABLE_ID='" + o.getTableId() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getCarRentRateId());
            stmt.setInt(2, o.getPerHour());
            stmt.setInt(3, o.getMinimumCharge());
            stmt.setInt(4, o.getSedan());
            stmt.setInt(5, o.getVan());
            stmt.setInt(6, o.getTrip());
            stmt.setInt(7, o.getServiceType());
            stmt.setString(8, o.getUpdateBy());
            stmt.setTimestamp(9, func.getTimestamp());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteByTableId(String tableId) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from CAR_RENT where TABLE_ID = '" + tableId.trim() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteBySQL(String sql) {
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
}
